<template>
	<view class="container">
		<uni-section title="新增医嘱" type="line">
			<view style="margin:30rpx">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :modelValue="baseFormData" label-width="70px">
					<uni-forms-item label="ID">
						<uni-easyinput disabled v-model="baseFormData.bh" placeholder="请输入Id" />
					</uni-forms-item>
					<uni-forms-item label="患者姓名">
						<uni-easyinput disabled v-model="baseFormData.xm" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="性别">
						<uni-easyinput disabled v-model="baseFormData.xb" placeholder="请输入性别" />
					</uni-forms-item>
					<uni-forms-item label="年龄">
						<uni-easyinput disabled v-model="baseFormData.nl" placeholder="请输入年龄" />
					</uni-forms-item>
					<uni-forms-item label="套餐">
						<view style="display: flex;align-items: center;">
							<uni-data-select @change="change" class="titleInput" v-model="baseFormData.office"
								:localdata="typeArrOffice" placeholder="请选择套餐" />
							<button @tap="manual"
								style="width: 120rpx;height: 68rpx;margin:10rpx;background-color: #274d20;color: #FFFFFF;font-size: 26rpx;line-height: 68rpx;">手动</button>

							<image @tap="viewOpen()" src="../../static/icons/check.png"
								style="width:50rpx ; height: 50rpx; " mode="widthFix"></image><text
								@tap.stop="viewOpen()"></text>


						</view>
					</uni-forms-item>


				</uni-forms>
			</view>
		</uni-section>
		<!-- 普通弹窗 -->
		<uni-popup ref="popup" background-color="#fff" @change="change">
			<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"></view>
		</uni-popup>
		<uni-section title="医嘱列表" type="line">
			<uni-collapse accordion @change="changeHandle" style="" v-if="back.length">
				<uni-collapse-item style="height: 88rpx; line-height: 88rpx;" :title="index+1+'.医嘱'"
					v-for="(item,index) in back" :key="index">
					<template v-slot:title>
						<view style="display: flex;align-items: center;justify-content: space-between;">
							<text style="margin:  0rpx 20rpx; font-size: 30rpx;">{{index+1}}.医嘱</text>

							<image @tap.stop="delOrders(item)" v-show="isOpenIndex==''?true:index <= isOpenIndex"
								src="../../static/icons/del.png" mode="widthFix" style="width: 48rpx;"></image>

						</view>

					</template>
					<view style="margin: 30rpx;padding-bottom: 200rpx;">
						<uni-forms :rules="rules" ref="baseForm" :modelValue="item" label-width="70px">
							<uni-forms-item label="长/临">
								<uni-data-select v-model="item.cl" :localdata="longOrPro" placeholder="请选择长期/临时医嘱" />
							</uni-forms-item>
							<uni-forms-item label="分类">
								<uni-data-select v-model="item.typeId" :localdata="classify" placeholder="请选择分类" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1" label="医嘱内容" name="yzlr">
								<uni-data-select v-model="item.ypId" :localdata="ordersContent"
									placeholder="请选择医嘱内容" />
							</uni-forms-item>
							<uni-forms-item v-else label="医嘱内容">
								<uni-data-select v-model="item.ypId" :localdata="ordersContent"
									placeholder="请选择医嘱内容" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1" label="剂量">
								<uni-number-box class="dosage" v-model="item.sl" @change="changeValue" />
							</uni-forms-item>
							<uni-forms-item v-else label="剂量">
								<uni-number-box disabled class="dosage" v-model="item.sl" @change="changeValue" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1" label="剂量单位">
								<uni-data-select v-model="item.dw" :localdata="dosageUnit" placeholder="请选择剂量单位" />
							</uni-forms-item>
							<uni-forms-item v-else label="剂量单位">
								<uni-data-select disabled v-model="item.dw" :localdata="dosageUnit"
									placeholder="请x选择剂量单位" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1" label="用量">
								<uni-number-box class="useDosage" v-model="item.yl" @change="changeValue" />
							</uni-forms-item>
							<uni-forms-item v-else label="用量">
								<uni-number-box disabled class="useDosage" v-model="item.yl" @change="changeValue" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1 && item.cl=='长期医嘱'" label="频次">
								<uni-data-select v-model="item.pc" :localdata="frequency" placeholder="请选择频次" />
							</uni-forms-item>
							<uni-forms-item v-else label="频次">
								<uni-data-select disabled v-model="item.pc" :localdata="frequency"
									placeholder="请选择频次" />
							</uni-forms-item>
							<uni-forms-item v-if="item.typeId==1" label="途径">
								<uni-data-select v-model="item.tj" :localdata="way" placeholder="请选择途径" />
							</uni-forms-item>
							<uni-forms-item v-else label="途径">
								<uni-data-select disabled v-model="item.tj" :localdata="way" placeholder="请选择途径" />
							</uni-forms-item>
							<uni-forms-item label="医嘱记录">
								<uni-easyinput v-model="item.bz" type="textarea" placeholder="请输入记录" />
							</uni-forms-item>
						</uni-forms>
					</view>
				</uni-collapse-item>

			</uni-collapse>
			<view style="text-align: center;color:#999999;padding: 30rpx;" v-else>
				暂无医嘱列表数据
			</view>
			<!-- <uni-list >
						<uni-list-item  :title="item.table" :rightText="item.type" />
					</uni-list> -->
		</uni-section>
		<view class="fixedBottom">
			<button @tap="woundedSave()">保存</button>
			<button @tap="woundedList()">医嘱列表</button>
		</view>
	</view>
</template>

<script>
	import {
		packageQuery,
		dictionary,
		OrdersQuery,
		contentOrders,
		addOrders
	} from '../../api/huanzhe.js'
	import {
		log
	} from '../../common/util.js';
	import {
		toast
	} from '../../utils/utils.js'
	export default {
		data() {
			return {
				rules: {
					yzlr: {
						rules: [{
							required: true,
							errorMessage: '请选择医嘱内容'
						}],
					},
				},
				isOpenIndex: '6',
				// 医嘱列表
				back: [],
				// 途径
				way: [],
				// 频次
				frequency: [],
				// 计量单位
				dosageUnit: [],
				// 医嘱内容
				ordersContent: [],
				// 分类
				classify: [],
				// 长/临下拉
				longOrPro: [],
				// 套餐下拉
				typeArrOffice: [],
				// 数据参数
				baseFormData: {},
				// 套餐医嘱详情
				baseOrders: [],
				// 患者id
				hzId: null,
				// 新增参数
				parameter: [],
				contentOrders: {
					pageNum: 1,
					pageSize: 100,
					bz: -1
				},
				wzName: null,
				Boolean:false
			};
		},
		onLoad(option) {
			this.baseFormData = option
			this.hzId = option.id
			this.queryPackage()
			this.dropDown()
		},
		methods: {
			changeValue(e) {
				console.log(e);
			},
			changeHandle(e) {
				console.log(typeof(e), e);
				this.isOpenIndex = e
			},
			// 下拉框
			dropDown() {
				contentOrders(this.contentOrders).then(res => {
					res.rows.forEach((item, index) => {
						var ordersContent = {
							value: item.id,
							text: item.mc
						}
						this.ordersContent.push(ordersContent)
					})

				})
				// 长/临
				dictionary('weiqin_yizhu_leixing').then(res => {
					res.data.forEach((item, index) => {
						var longOrPro = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.longOrPro.push(longOrPro)
					})
				})
				// 频次
				dictionary('weiqin_yizhu_pinci').then(res => {
					res.data.forEach((item, index) => {
						var frequency = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.frequency.push(frequency)
					})
				})
				// 剂量单位
				dictionary('weiqin_yizhu_jiliang_danwei').then(res => {
					res.data.forEach((item, index) => {
						var dosageUnit = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.dosageUnit.push(dosageUnit)
					})
				})
				// 分类
				dictionary('weiqin_yizhu_fenlei').then(res => {
					res.data.forEach((item, index) => {
						var classify = {
							value: item.dictLabel,
							text: item.dictValue
						}
						this.classify.push(classify)
					})

				})
				// 途径
				dictionary('weiqin_yizhu_tujing').then(res => {
					res.data.forEach((item, index) => {
						var way = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.way.push(way)
					})
				})
			},
			// 查询医嘱
			queryPackage() {
				packageQuery().then(res => {
					res.rows.forEach((item, inde) => {
						var query = {
							value: item.id,
							text: item.mc
						}
						this.typeArrOffice.push(query)
					})
					this.baseOrders = res.rows
				})
			},
			// 查看已开医嘱
			viewOpen() {
				uni.navigateTo({
					url: '/pages/casualtyList/viewOpenOrders?hzId=' + this.hzId
				})
			},
			// 保存
			woundedSave() {
				this.back.forEach((item, index) => {
					console.log(item);
					this.ordersContent.forEach((item1, index) => {
						if (item1.value === item.ypname) {
							this.wzName = item1.text
						}
					})
					var addquery = {
						bz: item.bz,
						ckfs: '本组',
						cl: item.cl,
						hzId: this.hzId,
						jl: item.sl,
						jldw: item.dw,
						lb: item.typeId,
						nr: this.wzName,
						pc: item.pc,
						tj: item.tj,
						wzId: item.ypId,
						yl: 1
					}
					console.log(this.Boolean);
					console.log(addquery.wzId);
					if(!addquery.wzId){
						console.log("dsasd");
						return this.Boolean=true
					}
					this.parameter.push(addquery)
				})
				if(this.Boolean){
					this.Boolean=false
					return toast("请输入医嘱内容")
				}
				addOrders(this.parameter).then(res => {
					this.parameter=[]
					if (res.code === 200) {
						uni.redirectTo({
							url: '/pages/doctorsAdvice/index'
						})
					}
					toast(res.msg)
				})
			},
			// 医嘱列表
			woundedList() {
				uni.redirectTo({
					url: '/pages/doctorsAdvice/index'
				})
			},
			delOrders(cur) {
				this.baseOrders = []
				this.typeArrOffice = []
				this.queryPackage()
				this.back.forEach((item, index) => {
					if (item.xmid == cur.xmid) {
						this.back.splice(index, 1)
					}
				})
			},
			manual() {

				
				this.back.push({
					xmid:new Date().getTime()
				})
				this.isOpenIndex = this.back.length

			},
			// 列表打开医嘱详情
			detail(e) {
				this.baseFormData.longOrPro = e.id
				this.baseFormData.classify = e.id
				this.$refs.popup.open("bottom")
			},
			change(e) {
				console.log(this.baseOrders);
				this.baseOrders.forEach((item, index) => {
					if (item.id == e) {
						this.isOpenIndex=item.yzxm.length
						this.back=[]
						this.back = item.yzxm
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	::v-deep.uni-section .uni-section-header__decoration {
		margin-right: 6px;
		background-color: #23440d;
	}

	.container {
		padding-bottom: 800rpx;

		::v-deep.uni-collapse-item__title-text {
			position: relative;

			&::before {
				content: '';
				width: 48rpx;
				height: 48rpx;
				// background-color: #fff;
				position: absolute;
				right: 0;
				top: 50%;
				margin-top: -24rpx;
				background: url('../../static/icons/del.png') no-repeat;
				background-size: cover;

			}
		}

		::v-deep.uni-forms-item__content {
			display: flex !important;
			align-items: center !important;
			justify-content: space-between !important;
		}
	}

	.popup-content {
		align-items: center;
		justify-content: center;
		padding: 15px;
		height: 100%;
		background-color: #fff;
	}

	.popup-height {
		width: 200px;
	}



	uni-button:after {
		outline: none;
		border: none !important;
	}

	.fixedBottom {
		height: 200rpx;
		background-color: #FFF;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 50;

		button {
			width: 300rpx;
			height: 88rpx;
			color: white;
			background-color: #23440d;
			border-radius: 20px;
			line-height: 88rpx;
			text-align: center;
		}
	}
</style>